﻿Public Class frmEmployeeDetail

#Region "Method"

#Region "Overrides"

    ''' <summary>
    ''' Sub đưa dữ liệu hiển thị trên control
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Protected Overrides Sub BindingControl()
        With BusinessObject.dsFMS.Employee
            LoadRole()
            txtAlias.DataBindings.Add("Text", bsFMS, .EmployeeAliasColumn.ColumnName)
            txtName.DataBindings.Add("Text", bsFMS, .EmployeeNameColumn.ColumnName)
            dteDateOfBirth.DataBindings.Add("Text", bsFMS, .EmployeeDOBColumn.ColumnName)
            txtPhone.DataBindings.Add("Text", bsFMS, .EmployeePhoneColumn.ColumnName)
            txtEmail.DataBindings.Add("Text", bsFMS, .EmployeeEmailColumn.ColumnName)
            txtAddress.DataBindings.Add("Text", bsFMS, .EmployeeAddressColumn.ColumnName)
            txtDescription.DataBindings.Add("Text", bsFMS, .EmployeeDescriptionColumn.ColumnName)
            txtUserName.DataBindings.Add("Text", bsFMS, .EmployeeLoginNameColumn.ColumnName)
            txtPassword.DataBindings.Add("Text", bsFMS, .EmployeePasswordColumn.ColumnName)
            ptbImage.DataBindings.Add("Image", bsFMS, .EmployeePhotoColumn.ColumnName)
            If ActionMode <> clscommon.ActionMode.Add Then
                cboRole.Value = bsFMS.Current(.RoleIDColumn.ColumnName)
                cboGender.Value = bsFMS.Current(.EmployeeGenderColumn.ColumnName)
                LoadInvoiceList()
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub xóa binding dữ liệu trên control
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Protected Overrides Sub ClearDataBingding()
        With Me
            .txtAlias.DataBindings.Clear()
            .txtName.DataBindings.Clear()
            .dteDateOfBirth.DataBindings.Clear()
            .txtPhone.DataBindings.Clear()
            .txtEmail.DataBindings.Clear()
            .txtAddress.DataBindings.Clear()
            .txtDescription.DataBindings.Clear()
            .txtUserName.DataBindings.Clear()
            .txtPassword.DataBindings.Clear()
            .ptbImage.DataBindings.Clear()
        End With
    End Sub

    ''' <summary>
    ''' Sub set thuộc tính cho các control là read only hay không
    ''' </summary>
    ''' <param name="isReadOnly">Ở chế độ read only thì đặt là true và ngược lại</param>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Protected Overrides Sub SetControlEditable(ByVal isReadOnly As Boolean)
        MyBase.SetControlEditable(isReadOnly)
        With Me
            .txtAlias.ReadOnly = isReadOnly
            .txtName.ReadOnly = isReadOnly
            .dteDateOfBirth.ReadOnly = isReadOnly
            .cboGender.ReadOnly = isReadOnly
            .txtPhone.ReadOnly = isReadOnly
            .txtEmail.ReadOnly = isReadOnly
            .txtAddress.ReadOnly = isReadOnly
            .txtDescription.ReadOnly = isReadOnly
            .txtUserName.ReadOnly = isReadOnly
            .txtPassword.ReadOnly = isReadOnly
            .lblRetypePassword.Visible = Not isReadOnly
            .txtRetypePassword.Visible = Not isReadOnly
            .btnImage.Visible = Not isReadOnly
            If ActionMode = clscommon.ActionMode.Add Then
                If cboRole.Items.Count > 0 Then
                    cboRole.SelectedIndex = 0
                Else
                    cboRole.Clear()
                End If
                    cboGender.SelectedIndex = 0
                    BusinessObject.dsFMS.Invoice.Clear()
            ElseIf ActionMode = clscommon.ActionMode.Edit Then
                txtRetypePassword.Text = bsFMS.Current(BusinessObject.dsFMS.Employee.EmployeePasswordColumn.ColumnName)
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub xác định vị trí của bản ghi hiện thời và load lại dữ liệu
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 24/10/2010</remarks>
    Protected Overrides Sub RefreshData()
        MyBase.RefreshData()
        If Not IsNothing(bsFMS.Current) And ActionMode <> clscommon.ActionMode.Add Then
            cboRole.Value = bsFMS.Current(BusinessObject.dsFMS.Employee.RoleIDColumn.ColumnName)
            cboGender.Value = bsFMS.Current(BusinessObject.dsFMS.Employee.EmployeeGenderColumn.ColumnName)
            LoadInvoiceList()
        End If
    End Sub

    ''' <summary>
    ''' Sub đặt giá trị cho các trường trước khi lưu dữ liệu
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 25/06/2012</remarks>
    Protected Overrides Sub SetDataValue()
        With BusinessObject
            If txtPassword.Text <> txtRetypePassword.Text Then
                clscommon.ShowException(My.Resources.PasswordError)
                txtPassword.Focus()
                txtPassword.SelectAll()
                Exit Sub
            Else
                .CurrentRow(.dsFMS.Employee.RoleIDColumn.ColumnName) = cboRole.Value
                .CurrentRow(.dsFMS.Employee.EmployeeGenderColumn.ColumnName) = cboGender.Value
                If ptbImage.Image.GetType() <> .CurrentRow(.dsFMS.Employee.EmployeePhotoColumn.ColumnName).GetType() Then
                    .CurrentRow(.dsFMS.Employee.EmployeePhotoColumn.ColumnName) = ImageToByte(ptbImage.Image)
                End If
            End If
        End With
    End Sub
#End Region

    ''' <summary>
    ''' Sub lấy dữ liệu từ bảng Role đưa vào dataset và hiển thị trên cboRole
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 27/06/2012</remarks>
    Private Sub LoadRole()
        With BusinessObject
            .dsFMS.Role.Clear()
            .GetTableData(.dsFMS.Role.TableName)
            cboRole.DataSource = .dsFMS.Role
            cboRole.DisplayMember = .dsFMS.Role.RoleNameColumn.ColumnName
            cboRole.ValueMember = .dsFMS.Role.RoleIDColumn.ColumnName
            If cboRole.Items.Count > 0 Then
                cboRole.SelectedIndex = 0
            Else
                cboRole.Clear()
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub đưa danh sách các hóa đơn khách hàng đã mua lên grdInvoiceList
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 25/06/2012</remarks>
    Private Sub LoadInvoiceList()
        With BusinessObject
            .dsFMS.Invoice.Clear()
            .GetDataByValue(.dsFMS.Invoice.TableName, .dsFMS.Employee.EmployeeIDColumn.ColumnName, bsFMS.Current(.dsFMS.Employee.EmployeeIDColumn.ColumnName))
            grdInvoiceList.DataSource = .dsFMS
            grdInvoiceList.DataMember = .dsFMS.Invoice.TableName
        End With
    End Sub

    ''' <summary>
    ''' chuyển đổi dữ liệu dạng image thành Byte
    ''' </summary>
    ''' <param name="img"></param>
    ''' <returns></returns>
    ''' <remarks>Nguyễn Huy Thường added on 25/06/2012</remarks>
    Private Function ImageToByte(ByVal img As Image) As Byte()
        Dim imgStream As System.IO.MemoryStream = New System.IO.MemoryStream
        img.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png)
        imgStream.Close()
        Return imgStream.ToArray()
    End Function
#End Region

#Region "Event"

    ''' <summary>
    ''' Thay đổi ảnh nhân viên
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 25/06/2012</remarks>
    Private Sub btnImage_Click(sender As System.Object, e As System.EventArgs) Handles btnImage.Click
        Try
            dlgSelectImage.ShowDialog()
            If Not String.IsNullOrEmpty(dlgSelectImage.FileName) Then
                ptbImage.DataBindings.Clear()
                ptbImage.Image = Image.FromStream(dlgSelectImage.OpenFile)
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
#End Region
End Class
